# -*- coding: utf-8 -*-
import scrapy
import time
from anjuke.items import AnjukeItem

class AnjuSpider(scrapy.Spider):
    name = 'anju'
    allowed_domains = ['anjuke.com']
    start_urls = ['https://cs.anjuke.com/tycoon/p1/']

    #定义需要爬取的页面数量，以及爬取链接
    def start_requests(self):
		reqs = []
		for i in range(1,137):#抓取的页面页码数量，
			req = scrapy.Request('https://cs.anjuke.com/tycoon/p%s/'%i)
			reqs.append(req)
		return reqs

    def parse(self, response):
    	item = AnjukeItem()
    	#所有的需要的数据都在id="list-content"的div节点下
        #div = response.xpath('//*[@id="list-content"]/div')
        #print len(div)
        print "***********"

        #优秀经纪人姓名
    	item['name'] = response.xpath("//*[@id='list-content']/div/div[1]/div[1]/h3/a/text()").extract()
    	#print name[0]

    	#优秀经纪人等级评分
    	item['grade'] = response.xpath("//*[@id='list-content']/div/div[1]/div[1]/div[1]/div[2]/p/text()").extract()
    	#print grade[0]

    	#优秀经纪人房源描述评分
    	item['fang_yuan'] = response.xpath('//*[@id="list-content"]/div/div[1]/div/div[2]/div/div[1]/span[1]/em[2]/text()').extract()
    	#print fang_yuan[0]
    	#优秀经纪人房源描述打败**同城经纪人
    	item['fang_yuan_com'] = response.xpath('//*[@id="list-content"]/div/div[1]/div/div[2]/div/div[2]/div[1]/span[2]/text()').extract()
    	#print fang_yuan_com[0]

    	#优秀经纪人服务态度评分
    	item['fu_wu'] = response.xpath('//*[@id="list-content"]/div/div[1]/div/div[2]/div/div[1]/span[2]/em[2]/text()').extract()
    	#print fu_wu[0]
    	#优秀经纪人服务态度打败**同城经纪人
    	item['fu_wu_com'] = response.xpath('//*[@id="list-content"]/div/div[1]/div/div[2]/div/div[2]/div[2]/span[2]/text()').extract()
    	#print fu_wu_com[0]

    	#优秀经纪人信用评价评分
    	item['ping_jia'] = response.xpath('//*[@id="list-content"]/div/div[1]/div/div[2]/div/div[1]/span[3]/em[2]/text()').extract()
    	#print ping_jia[0]
    	#优秀经纪人信用评价打败**同城经纪人
    	item['ping_jia_com'] = response.xpath('//*[@id="list-content"]/div/div[1]/div/div[2]/div/div[2]/div[3]/span[2]/text()').extract()
    	#print ping_jia_com[0]

    	#优秀经纪人电话号码
    	item['phone_num'] = response.xpath('//*[@id="list-content"]/div/div[2]/text()').extract()
    	#print phone_num[1]

    	#print item
    	print "********************"
        return item
